(function($){

    $.fn.extend({
        fileuploader : function(options, arg) {
            if (typeof(options) == "object") {
                options = $.extend({}, $.fileuploader.defaults, options);
            }
            this.each(function() {
                for (var fld in options) {
                    jQuery(this).attr("plugin_fileuploader_" + fld, options[fld]);
                }
                new $.fileuploader(this, options, arg);
            });
            return;
        }
    });

    $.fileuploader = function(item, options, arg) {
        if (typeof(options) == "string") {
            var methodName = options;
            options = {};
            for (var fld in $.fileuploader.defaults) {
                options[fld] = jQuery(item).attr("plugin_fileuploader_" + fld);
            }
            // methods
        } else {
            jQuery(item).find("img").each(function(ndx, elem) {
                jQuery(elem).click(function(ev) {
                    var e = ev || window.event;
                    displayLargeImage(elem, e.clientX, e.clientY);
                });
            });
        }

        $.fileuploader.defaults = {
        }
    }
})(jQuery);

function fileuploader_enable(item) {
    if (typeof(item) == "string") {
        jQuery("table#" + item + " button.uploadbtn").show( "fade", {}, 300);
    } else {
        jQuery(item).find("button.uploadbtn").show("fade", {}, 300);
    }
}

function fileuploader_disable(item) {
    if (typeof(item) == "string") {
        jQuery("table#" + item + " button.uploadbtn").hide( "fade", {}, 300);
    } else {
        jQuery(item).find("button.uploadbtn").hide("fade", {}, 300);
    }
}

function fileuploader_setValue(name, value) {
    jQuery("input[type='hidden'][name='" + name + "']").val(value);
    if (value != "") {
        jQuery("img[name='preview_" + name + "']").attr("src", "index.php?mode=service&page=fileloader&service=display&_file_id=" + value);
    } else {
        jQuery("img[name='preview_" + name + "']").attr("src", "#");
    }
}

function fileuploader_uploaded(parameters, popupResult, onchangeCallback) {
    if (onchangeCallback) { onchangeCallback(popupResult.logofileid); }
    jQuery("img[name='preview_" + parameters.popupId + "']").attr("src", "index.php?mode=service&page=fileloader&service=display&_file_id=" + popupResult.logofileid)
    .click(function(ev) {
    });
    jQuery("input[name='" + parameters.popupId + "']").val(popupResult.logofileid);
}
